package com.sbtr.controller.classify;

import cn.hutool.json.JSONUtil;
import com.sbtr.business.classify.entity.AlgoVersionFile;
import com.sbtr.business.classify.vo.AlgoClipRecordNodeVO;
import com.sbtr.common.Response;
import com.sbtr.common.file.FileService;
import com.sbtr.util.BeanCopyUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.*;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.sbtr.common.PageDto;
import com.sbtr.business.classify.entity.AlgoClipRecord;
import com.sbtr.business.classify.dto.AlgoClipRecordDto;
import com.sbtr.business.classify.service.AlgoClipRecordService;
import org.springframework.web.bind.annotation.RestController;
import com.sbtr.controller.common.CommonController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 算法生成 前端控制器
 *
 * @author dgl
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/algoClipRecord")
@Api(tags = "算法裁剪生成记录")
public class AlgoClipRecordController extends CommonController<AlgoClipRecord,AlgoClipRecordService> {

    @Autowired
    FileService fileService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页列表")
    public Response<PageDto<AlgoClipRecordNodeVO>> pageList(AlgoClipRecordDto algoClipRecordDto){
        PageDto<AlgoClipRecord> pageDto = u.pageList(algoClipRecordDto);
        PageDto<AlgoClipRecordNodeVO> pageDto1=BeanCopyUtils.copyProperties(pageDto,PageDto.class);
        List<AlgoClipRecord> list = pageDto.getList();
        List<AlgoClipRecordNodeVO> list1 = new ArrayList<>();
        line:
        for (AlgoClipRecord old:list){
            AlgoClipRecordNodeVO r=BeanCopyUtils.copyProperties(old,AlgoClipRecordNodeVO.class);
            r.setChildren(JSONUtil.parseArray(old.getConditionJson()));
            list1.add(r);
        }
        pageDto1.setList(list1);
        return Response.success(pageDto1);
    }

    @PostMapping("/download")
    @ApiOperation(httpMethod = "POST", value = "下载记录")
    public void download(HttpServletResponse resp,Long id){
        AlgoClipRecord algoClipRecord=u.getById(id);
        fileService.downloadFile(resp, algoClipRecord.getClipFile(), algoClipRecord.getFileName());
    }


    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "保存")
    public Response<AlgoClipRecord> save(@RequestBody AlgoClipRecord algoClipRecord){
        algoClipRecord.setId(null);
        if(algoClipRecord.insert()){
            return Response.success(algoClipRecord);
        }else throw new BDException("保存失败");
    }

}

